package com.vanzo.nfc;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NfcAdapter;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.vanzo.encryption.Encryption;
import com.vanzo.encryption.R;
import com.vanzo.talkie.Follower;

import java.util.ArrayList;
import java.util.List;

public class SendActivity extends AppCompatActivity {

    public static final String TAG = "SendActivity";

    NFCManager nfcMger;
    private TextView text;
    private List<Follower> followers = new ArrayList<>();
    private int position = 0;
    private byte[] gk = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.text = (TextView) findViewById(R.id.sample_text);
        nfcMger = new NFCManager(this);
        nfcMger.setCallback(createBeamUrisCallback, createNdefMessageCallback, onNdefPushCompleteCallback);
        initSends.sendEmptyMessage(0);
    }

    private int groupId = 3;
    private int followersCount = 5;

    private Handler initSends = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            gk = Encryption.getGroupKey();
            int i = 1;
            while (i <= followersCount) {
                Follower follower = new Follower(gk, Encryption.getKillKey(), groupId, i, followersCount, 1, 1, 1);
                if (follower.getLength() > 2048) {
                    continue;
                } else {
                    followers.add(follower);
                    i++;
                }
            }
        }
    };
    private NfcAdapter.CreateBeamUrisCallback createBeamUrisCallback = new NfcAdapter.CreateBeamUrisCallback() {
        @Override
        public Uri[] createBeamUris(NfcEvent event) {
            return new Uri[0];
        }
    };

    private NfcAdapter.CreateNdefMessageCallback createNdefMessageCallback = new NfcAdapter.CreateNdefMessageCallback() {
        @Override
        public NdefMessage createNdefMessage(NfcEvent event) {
//            text.setText("发送的数据"+followers.get(position).toString());
            Message msg = new Message();
            msg.obj = "发送的数据" + followers.get(position).toString();
            sendTextHandler.sendMessage(msg);
            return nfcMger.createTextMessage(followers.get(position));
        }
    };

    private NfcAdapter.OnNdefPushCompleteCallback onNdefPushCompleteCallback = new NfcAdapter.OnNdefPushCompleteCallback() {
        @Override
        public void onNdefPushComplete(NfcEvent event) {
            position++;
            position = position % followers.size();
        }
    };

    private Handler sendTextHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            text.setText((String) msg.obj);
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        try {
            nfcMger.verifyNFC();
            nfcMger.startBind();
        } catch (NFCManager.NFCNotSupported nfcnsup) {
            Toast.makeText(this, R.string.nfc_unsupport, Toast.LENGTH_SHORT).show();
        } catch (NFCManager.NFCNotEnabled nfcnEn) {
            // deal with nfc not enable error
            // ask open nfc or finish activity;
//            Toast.makeText(this, "nfc not enable", Toast.LENGTH_SHORT).show();
            AlertDialog.Builder b = new AlertDialog.Builder(this);
            b.setTitle(R.string.nfc_unenable);
            b.setMessage(R.string.open_nfc);
            b.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent();
                    intent.setAction(Settings.ACTION_NFC_SETTINGS);
                    startActivity(intent);
                }
            });
            b.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            b.show();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
}
